package org.game.module.event.listener;

import java.util.List;

import org.constant.EventConditionKey;
import org.constant.EventConditionType;
import org.constant.EventType;
import org.game.constant.DungeonConstant;
import org.game.db.bean.DungeonLevel;
import org.game.db.bean.Player;
import org.game.plugin.event.GameEvent;
import org.game.plugin.event.GameEventListener;
import org.game.plugin.event.GameEventStack;
import org.game.plugin.event.GameEventType;

import com.sojoys.artifact.tools.ToolMap;

@GameEventType(EventType.PASS_DUP)
public class DupFinishListener implements GameEventListener {

	@Override
	public void onEvent(GameEvent event) {
		GameEventStack eventStack = event.peek();
		switch (eventStack.getConditionType()) {
		case EventConditionType.FIGHT_EVENT: {
			boolean bool = true;
			bool = checkParams(event, eventStack, bool);
			if (bool) {
				eventStack.changeProgress(1);
			}
			break;
		}
		case EventConditionType.FIGHT_TOTAL_STAR: {
			int addStar = ToolMap.getInt(EventConditionKey.ADD_STAR, event.getIn());
			boolean bool = addStar > 0;
			bool = checkParams(event, eventStack, bool);
			if (bool) {
				eventStack.changeProgress(addStar);
			}
			break;
		}
		case EventConditionType.PERFECT_DUN: {
			boolean bool = ToolMap.getBoolean(EventConditionKey.FIRST_3_STAR, event.getIn());
			bool = checkParams(event, eventStack, bool);
			if (bool) {
				eventStack.changeProgress(1);
			}
			break;
		}
		case EventConditionType.FIGHT_BATTER: {
			int batter = ToolMap.getInt(EventConditionKey.BATTER, event.getIn(),0);
			boolean bool = batter > 0;
			bool = checkParams(event, eventStack, bool);
			if (bool && batter > eventStack.getMaxProgress()) {
				// 设置最大连击进度
				eventStack.setProgress(batter);
			}
			break;
		}
		case EventConditionType.FIGHT_PICKUP_COUNT: {
			int pickUpCount = ToolMap.getInt(EventConditionKey.PICK_UP_COUNT, event.getIn(),0);
			boolean bool = pickUpCount > 0;
			bool = checkParams(event, eventStack, bool);
			if (bool && pickUpCount > eventStack.getMaxProgress()) {
				// 设置最大连击进度
				eventStack.changeProgress(pickUpCount);
			}
			break;
		}
		case EventConditionType.FIGHT_PICKUP_TYPE_COUNT: {
			int pickUpTypeCount = ToolMap.getInt(EventConditionKey.PICK_UP_TYPE_COUNT, event.getIn(),0);
			boolean bool = pickUpTypeCount > 0;
			bool = checkParams(event, eventStack, bool);
			if (bool && pickUpTypeCount >= eventStack.getProgress()) {
				// 设置最大连击进度
				eventStack.changeProgress(pickUpTypeCount);
			}
			break;
		}
		case EventConditionType.FIGHT_ASSIGN_DUP:{
			boolean bool = true;
			bool = checkParams(event, eventStack, bool);
			if (bool) {
				eventStack.changeProgress(1);
			}
		}
		}
	}

	/**
	 * 条件参数检查
	 * 
	 * @param event
	 * @param eventStack
	 * @param bool
	 * @return
	 */
	private boolean checkParams(GameEvent event, GameEventStack eventStack, boolean bool) {
		bool &= verify_eq(EventConditionKey.DUNGEON_CID, event.getIn(), eventStack.getParams());
		bool &= verify_ge(EventConditionKey.STAR, event.getIn(), eventStack.getParams());
		bool &= verify_le(EventConditionKey.FIGHT_HERO_COUNT, event.getIn(), eventStack.getParams());
		bool &= verify_le(EventConditionKey.FIGHT_TIME, event.getIn(), eventStack.getParams());
		bool &= verify_eq(EventConditionKey.DIFFICULTY, event.getIn(), eventStack.getParams());
		bool &= verify_eq(EventConditionKey.DUNGOEN_TYPE, event.getIn(), eventStack.getParams());
		return bool;
	}

	@Override
	public void initEventCondition(Player player, GameEventStack eventStack) {
		switch (eventStack.getConditionType()) {
		case EventConditionType.FIGHT_EVENT: {
			break;
		}
		case EventConditionType.FIGHT_TOTAL_STAR: {
			List<DungeonLevel> list = player.getDungeonLevelsFkPlayerId();
			int count = 0;
			for (DungeonLevel dungeonLevel : list) {
				count+=dungeonLevel.getStar();
			}
			eventStack.setProgress(count);
			break;
		}
		case EventConditionType.PERFECT_DUN: {
			List<DungeonLevel> list = player.getDungeonLevelsFkPlayerId();
			int count = 0;
			for (DungeonLevel dungeonLevel : list) {
				if (dungeonLevel.getStar() >= DungeonConstant.CHAPTER_MAX_STAR) {
					count++;
				}
			}
			eventStack.setProgress(count);
			break;
		}
		case EventConditionType.FIGHT_BATTER: {
			break;
		}
		case EventConditionType.FIGHT_ASSIGN_DUP:{
			int dunId = ToolMap.getInt(EventConditionKey.DUNGEON_CID, eventStack.getParams(),0);
			List<DungeonLevel> list = player.getDungeonLevelsFkPlayerId();
			int count = 0;
			for (DungeonLevel dungeonLevel : list) {
				if (dungeonLevel.getCid() == dunId) {
					count=1;
				}
			}
			eventStack.setProgress(count);
			break;
		}
		}
	}

}
